package com.init.iblog.controller;

import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.entity.User;
import com.init.iblog.enums.AppHttpCodeEnum;
import com.init.iblog.exception.SystemException;
import com.init.iblog.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户登录控制层
 */
@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;


    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResponseResult userLogin(@RequestBody User user){
        //前端需要做校验，后端也需要做校验(有些请求可能不是来源于前端页面的请求)
        System.out.println("++++++++++++++++进入/login查询所有友链接口++++++++++++++++");
        System.out.println("===============前端传来的参数user"+user.toString());
        if(!StringUtils.hasText(user.getName())){
            //提示  必须需要传入用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return loginService.userLogin(user);
    }


    /**
     * 用户退出登录
     * @return
     */
    @PostMapping("/logout")
    public ResponseResult logout(){
        System.out.println("++++++++++++++++进入/logout用户退出登录接口++++++++++++++++");
        return loginService.logout();
    }
}
